package com.koron.css2.meterService.impl;

import com.koron.css2.ImportExcelInterface;
import com.koron.css2.baseConfig.bean.MeterFactoryBean;
import com.koron.css2.baseConfig.bean.MeterModelBean;
import com.koron.css2.baseConfig.mapper.MeterFactoryMapper;
import com.koron.css2.baseConfig.mapper.MeterModelMapper;
import com.koron.css2.common.dto.HeaderInfoDto;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.util.Constant;
import org.koron.ebs.mybatis.SessionFactory;
import org.springframework.web.multipart.MultipartFile;
import org.swan.bean.MessageBean;

import java.util.*;
import java.util.Map.Entry;

/**
 * 水表变更批量导入
 * @author 59532
 *
 */
public class WmChgImport implements ImportExcelInterface{

	@Override
	public MessageBean<?> importExel(SessionFactory factory, UserInfoBean userInfo, RequestBean req,List<Map<String, String>> data, MultipartFile file) {
		@SuppressWarnings("rawtypes")
		MessageBean<Map> message = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "导入成功", Map.class);
		Map<String, Object> map = new HashMap<String, Object>();
		//水表厂商
        MeterFactoryMapper meterFactoryMapper = factory.getMapper(MeterFactoryMapper.class);
       //水表型号
      	MeterModelMapper meterModelMapper = factory.getMapper(MeterModelMapper.class);
		for(Map<String, String> m : data) {
			Set<Entry<String, String>> set = m.entrySet();
			Iterator<Entry<String, String>> iterator = set.iterator();
			while(iterator.hasNext()) {
				Entry<String, String> entry = iterator.next();
				String key = entry.getKey();
				if("meterFactory".equals(key)) {
					MeterFactoryBean meterFactory = meterFactoryMapper.selectMeterFactoryByName(entry.getValue());
					if(meterFactory != null) {
						m.put("meterFactory", meterFactory.getId());
					}else {
						m.put("meterFactory", "");
					}
				}
				if("meterModel".equals(key)) {
					MeterModelBean meterModel = meterModelMapper.selectMeterModelByName(entry.getValue());
					if(meterModel != null) {
						m.put("meterModel", meterModel.getId());
					}else {
						m.put("meterModel", "");
					}
				}
			}
		}
		map.put("list", data);
		message.setData(map);
		return message;
	}

	@Override
	public List<HeaderInfoDto> getHeaderInfo(UserInfoBean userInfo) {
		List<HeaderInfoDto> headerInfos = new ArrayList<HeaderInfoDto>();
		headerInfos.add(new HeaderInfoDto("水表编号", true, "meterNo"));
		headerInfos.add(new HeaderInfoDto("表身码", true, "factoryNo"));
		headerInfos.add(new HeaderInfoDto("水表厂家", true, "meterFactory"));
		headerInfos.add(new HeaderInfoDto("水表型号", true, "meterModel"));
		headerInfos.add(new HeaderInfoDto("IMEI", true, "imei"));
		headerInfos.add(new HeaderInfoDto("IMSI", true, "imsi"));
		headerInfos.add(new HeaderInfoDto("SIM", true, "sim"));
		headerInfos.add(new HeaderInfoDto("合格证号", true, "meterCert"));
		return headerInfos;
	}

	@Override
	public boolean isCheckHeader() {
		return true;
	}

}
